using System;
using Function.SkillEditor.Runtime;
using SCore.Editor.WindowDraw;
using SCore.Function.Fixed;
using Spine.Unity;
using UnityEditor;
using UnityEngine;

namespace Function.SkillEditor.Editor
{
    public class SkillSpineAnimClipDraw : ISkillClipDrawer
    {
        public override void OnInspector(Rect rect)
        {
            base.OnInspector(rect);
            EditorGUI.BeginChangeCheck();
            var clip = _skillClip as SkillSpineAnimClip;
            var lableWidth = EditorGUIUtility.labelWidth;
            EditorGUIUtility.labelWidth = 60;

            var skeletonDataAsset =
                EditorDrawHelp.DrawHObjetField(new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20),
                    "Object", _skillClip._asset as SkeletonDataAsset);

            clip._scaleX = (SpineScaleX)EditorGUI.EnumPopup(
                new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20), new GUIContent("朝向"), clip._scaleX);

            clip._layer = (AnimLayer)EditorGUI.EnumPopup(
                new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20), new GUIContent("层"), clip._layer);

            clip._animName = EditorGUI.TextField(
                new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20), new GUIContent("动画名"), clip._animName);

            if (GUI.Button(new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20), "自适应长度"))
            {
                var skeletonData = skeletonDataAsset.GetSkeletonData(true);
                if (skeletonData != null)
                {
                    var animation = skeletonData.FindAnimation(clip._animName);
                    if (animation != null)
                    {
                        _skillClip._lengthFrame = Mathf.RoundToInt(animation.Duration /
                                                                   SkillRunMap.Instance.GetEOneFrameTime()
                                                                       .AsFloat());
                        _skillClip._endFrame = _skillClip._startFrame + _skillClip._lengthFrame;
                        RefreshArea();
                        _skillRunLine.RefreshMaxFrame();
                    }
                }
            }

            if (EditorGUI.EndChangeCheck())
            {
                if (SkillTimeLineSubWindow._editorGo != null)
                {
                    var skeletonAnimation = SkillTimeLineSubWindow._editorGo.GetComponent<SkeletonAnimation>();
                    if (skeletonAnimation != null)
                    {
                        skeletonAnimation.skeleton.ScaleX = (float)clip._scaleX;
                    }
                }
            }


            EditorGUIUtility.labelWidth = lableWidth;
        }

        protected override void ClipNameGUI()
        {
            var clip = _skillClip as SkillSpineAnimClip;
            if (clip._animName == string.Empty)
            {
                EditorGUI.LabelField(_rect, "animName missing",
                    EditorDrawHelp.GetTextGUIStyle(10, TextAnchor.MiddleCenter,
                        EditorDrawHelp._crimsonRed, FontStyle.Bold));
            }
            else
            {
                EditorGUI.LabelField(_rect, clip._animName,
                    EditorDrawHelp.GetTextGUIStyle(10, TextAnchor.MiddleCenter,
                        EditorDrawHelp._black, FontStyle.Bold));
            }
        }

        public override void OnProgress(bool isPlay, fp progress)
        {
            fp curPlayerTime =
                progress - _skillClip._startFrame * SkillRunMap.Instance.GetEOneFrameTime();

            var clip = _skillClip as SkillSpineAnimClip;
            var skeletonAnimation = SkillTimeLineSubWindow._editorGo.GetComponent<SkeletonAnimation>();
            skeletonAnimation.AnimationState.SetAnimation((int)clip._layer, clip._animName, false).TrackTime =
                curPlayerTime.AsFloat();
        }
    }

    [TrackDrawEditor(typeof(SkillSpineAnimTrack), typeof(SkillSpineAnimClipDraw))]
    public class SkillSpineAnimTrackDraw : ISkillTrackDrawer<SkillSpineAnimTrack>
    {
        public SkillSpineAnimTrackDraw(SkillSpineAnimTrack skillTrack, TrackRegisterAttribute trackRegister) : base(
            skillTrack, trackRegister)
        {
        }

        protected override void DrawGroup()
        {
            EditorGUI.LabelField(new Rect(_headRect.x + 10, _headRect.y, _headRect.width, _headRect.height), "Spine");
        }
    }
}